/*
 * @Author: szx
 * @Date: 2023-04-17 15:46:53
 * @LastEditTime: 2023-04-17 16:15:58
 * @Description: 
 * @FilePath: \learn\fullstackopen\part6\redux-anecdotes-main\src\services\anecdotes.js
 */
import axios from 'axios'

const baseUrl = 'http://localhost:3001/anecdotes'

const getAll = async () => {
    const response = await axios.get(baseUrl)
    return response.data
}

const createNew = async (content) => {
    const object = { content, votes: 0 }
    const response = await axios.post(baseUrl, object)
    return response.data
}

const updateAnecdoteVotes = async (id) => {
    const content = await axios.get(`${baseUrl}/${id}`)
    const anecdote = { ...content.data, votes: content.data.votes + 1 }
    const response = await axios.put(`${baseUrl}/${id}`, anecdote)
    return response.data
}


const exportObject = {
    getAll, createNew, updateAnecdoteVotes
}
export default exportObject